<?php
namespace app\user\controller;

use tp5_lib\user\model\UserModel;
use tp5_lib\user\logic\UserLogic;
use tp5_lib\user\logic\UserModule;
use app\user\validate\UserValidate;
use think\facade\Session;
use think\facade\Request;
use tp5_lib\user\controller\BaseLoginController;

/**
 * 登录控制器
 * 实现登录、注册、找回密码、绑定手机号等功能
 * 处理登录之后跳转到指定页面的行为
 * 
 * @property UserModel $model 用户模型
 * @property UserLogic $logic 用户逻辑
 * @property UserValidate $validate 用户验证器
 */
final class LoginController extends BaseLoginController
{
    public function __get($name)
    {
        if($name === "model"){
            if(empty($this->_model)){
                $this->_model=new UserModel();
            }
            return $this->_model;
        }
        elseif($name === "logic"){
            if(empty($this->_logic)){
                $this->_logic=new UserLogic();
            }
            return $this->_logic;
        }
        elseif($name === "validate"){
            if(empty($this->_validate)){
                $this->_validate=new UserValidate();
            }
            return $this->_validate;
        }
    }
    /**
     * 显示注册页面
     */
    public function register()
    {
        $this->markRedirectUrl();
        $template = $this->request->isMobile() ? "user@public/mobile/register" : "user@public/pc/register" ;
        $this->assign("outside",$this->outside());
        return $this->fetch($template);
    }
    /**
     * 注册
     * 昵称+密码
     * @api
     */
    public function doRegister()
    {
        $data=input();
        if(!$this->validate->scene("register")->check($data)){
            $this->error($this->validate->getError());
        }
        $user=$this->logic->register($data["username"],$data["password"],$data);
        if(!$user){
            $this->error($this->logic->getError());
        }
        $this->loginRedirect($user,"注册成功，正在跳转");
    }
    /**
     * 手机号注册
     * 手机号+短信验证码+密码
     * @api
     */
    public function doMobileRegister()
    {
        $data=input();
        if(!$this->validate->scene("mobile_register")->check($data)){
            $this->error($this->validate->getError());
        }
        $user=$this->logic->mobileRegister($data["mobile"],$data["code"],$data["password"]);
        if(!$user){
            $this->error($this->logic->getError());
        }
        $this->loginRedirect($user,"注册成功，正在跳转");
    }
    /**
     * 快速注册
     * 手机号+短信验证码
     * @api
     */
    public function doQuickRegister()
    {
        $data=input();
        if(!$this->validate->scene("quick_register")->check($data)){
            $this->error($this->validate->getError());
        }
        $user=$this->logic->quickRegister($data["mobile"],$data["code"]);
        if(!$user){
            $this->error($this->logic->getError());
        }
        $this->loginRedirect($user,"注册成功，正在跳转");
    }
    /**
     * 显示登录页面
     */
    public function login()
    {
        $this->markRedirectUrl();
        $template = $this->request->isMobile() ? "user@public/mobile/login" : "user@public/pc/login" ;
        $this->assign("outside",$this->outside());
        return $this->fetch($template);
    }
    /**
     * 登录
     * 用户ID/手机号/昵称+密码
     * @api
     */
    public function doLogin()
    {
        $data=input();
        if(!$this->validate->scene("login")->check($data)){
            $this->error($this->validate->getError());
        }
        $user=$this->logic->login($data["username"],$data["password"]);
        if(!$user){
            $this->error($this->logic->getError());
        }
        $this->loginRedirect($user,"登陆成功，正在跳转");
    }
    /**
     * 短信登录
     * 手机号+短信验证码
     * @api
     */
    public function doSmsLogin()
    {
        $data=input();
        if(!$this->validate->scene("sms_login")->check($data)){
            $this->error($this->validate->getError());
        }
        $user=$this->logic->smsLogin($data["mobile"],$data["code"]);
        if(!$user){
            $this->error($this->logic->getError());
        }
        $this->loginRedirect($user,"登陆成功，正在跳转");
    }
    /**
     * 绑定手机号
     */
    public function bindMobile()
    {
        $this->markRedirectUrl();
        $template = $this->request->isMobile() ? "user@public/mobile/bindMobile" : "user@public/pc/bindMobile" ;
        return $this->fetch($template);
    }
    /**
     * @api
     */
    public function doBindMobile()
    {
        $data=input();
        $result=$this->validate($data,["mobile"=>"require","code"=>"require"]);
        if(is_string($result)){
            $this->error($result);
        }
        $user_id=session("bind_mobile_user_id");
        if(!$user_id){
            $this->error("用户未登录","login");
        }
        $user=$this->logic->model->db()->where(["id"=>$user_id])->find();
        if(!$user=$this->logic->bindMobile($user,$data["mobile"],$data["code"])){
            $this->error($this->logic->getError());
        }
        $this->loginRedirect($user,"绑定成功，正在跳转");
    }
    public function jumpBindMobile()
    {
        $this->doLoginRedirect("游客身份访问");
    }
    /**
     * 找回密码
     * 手机号+短信验证码
     */
    public function getback()
    {
        $this->markRedirectUrl();
        $template = $this->request->isMobile() ? "user@public/mobile/getback" : "user@public/pc/getback" ;
        return $this->fetch($template);
    }
    /**
     * @api
     */
    public function doGetback()
    {
        $data=input();
        if(!$this->validate->scene("getback_password")->check($data)){
            $this->error($this->validate->getError());
        }
        if(!$this->logic->getbackPassword($data["username"],$data["code"],$data["new_password"])){
            $this->error($this->logic->getError());
        }
        $this->success("修改成功",url("login"));
    }
}
